package com.hyt.it.ogt.kq.service.gov.schedule.utils;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.hyt.it.ogt.kq.service.gov.schedule.model.ExamRoom;

public class ExamRoomUtils {

    /** 查找可编排的考场，要求当前考场有空坐位，考场支持考试科目 */
    public static ExamRoom findCanScheduleExamRoomBySubject(List<ExamRoom> examRoomList, String subject) {
        for (ExamRoom examRoom : examRoomList) {
            if (examRoom.getSubjects() != null && examRoom.getIdlePosition().size() > 0
                    && examRoom.getSubjects().contains(subject)) {
                if (examRoom.getConstraintSubject() == null) {
                    return examRoom;
                } else if (examRoom.getConstraintSubject().equalsIgnoreCase(subject)) {
                    return examRoom;
                }

            } else if (examRoom.getVirGroupName() != null && examRoom.getIdlePosition().size() > 0) {
                if (examRoom.getConstraintSubject() == null) {
                    return examRoom;
                } else if (examRoom.getConstraintSubject().equalsIgnoreCase(subject)) {
                    return examRoom;
                }
            }
        }
        return null;
    }

    /** 查找可编排的考场，要求当前考场有空坐位，考场支持考试科目 */
    public static ExamRoom findCanScheduleExamRoomBySubjects(List<ExamRoom> examRooms, List<String> subjects) {
        List<String> filterSubjects = subjects.stream().filter(item -> !item.equalsIgnoreCase("ANY"))
                .collect(Collectors.toList());

        List<ExamRoom> examRoomList = examRooms.stream().sorted(Comparator.comparing(item -> {
            return ((ExamRoom) item).getCapacity() - ((ExamRoom) item).getIdlePosition().size();
        }).reversed()).collect(Collectors.toList());

        for (ExamRoom examRoom : examRoomList) {
            if (examRoom.getSubjects() != null && examRoom.getIdlePosition().size() > 0
                   && examRoom.getSubjects().containsAll(filterSubjects)) {

                return examRoom;

            } else if (examRoom.getVirGroupName() != null && examRoom.getIdlePosition().size() > 0) {
                return examRoom;
            }
        }
        return null;
    }

    /** 查找可编排的考场，要求当前考场有空坐位，考场支持考试科目和现已占用的考试科目 */
    public static ExamRoom findCanScheduleExamRoomByAllSubjects(List<ExamRoom> examRooms, List<String> subjects) {
        List<String> filterSubjects = subjects.stream().filter(item -> !item.equalsIgnoreCase("ANY"))
                .collect(Collectors.toList());
        List<ExamRoom> examRoomList = examRooms.stream().sorted(Comparator.comparing(item -> {
            return ((ExamRoom) item).getCapacity() - ((ExamRoom) item).getIdlePosition().size();
        }).reversed()).collect(Collectors.toList());

        for (ExamRoom examRoom : examRoomList) {
            if (examRoom.getSubjects() != null && examRoom.getIdlePosition().size() > 0
                    && examRoom.getSubjects().containsAll(filterSubjects)) {
                if (SimilarUtils.canMerge(examRoom.getBatchSubjectList(), subjects)) {
                    return examRoom;
                }

            } else if (examRoom.getVirGroupName() != null && examRoom.getIdlePosition().size() > 0) {
                if (SimilarUtils.canMerge(examRoom.getBatchSubjectList(), subjects)) {
                    return examRoom;
                }
            }
        }
        return null;
    }

}
